<template>
    <el-dialog :visible.sync="signDialogVisible" style="width:800px; margin: 20px auto;">
        <h3 class="dialogTitle">用户注册</h3>
        <el-form ref="form" :model="form" :rules="rules" label-width="0px" style="width: 90%; margin: 0px auto">
            <el-form-item prop="name">
                <el-input v-model="form.name" auto-complete="on" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="newdepart">
                <el-cascader style="width: 100%"
                        v-model="form.newdepart"
                        :options="options"
                        :props="{ expandTrigger: 'hover' }"
                        @change="handleChange">
                </el-cascader>
            </el-form-item>
            <!--<el-form-item prop="department">-->
                <!--<el-select v-model="form.department" placeholder="请选择部门" style="width: 100%;">-->
                    <!--<el-option-->
                            <!--v-for="item in optionsDepart"-->
                            <!--:key="item.value"-->
                            <!--:label="item.label"-->
                            <!--:value="item.value">-->
                    <!--</el-option>-->
                <!--</el-select>-->
            <!--</el-form-item>-->
            <!--<el-form-item prop="level">-->
                <!--<el-select v-model="form.level" placeholder="请选择职位" style="width: 100%;"-->
                           <!--:disabled="valueDepart=='public'">-->
                    <!--<el-option-->
                            <!--v-for="item in optionsLevel"-->
                            <!--:key="item.value"-->
                            <!--:label="item.label"-->
                            <!--:value="item.value">-->
                    <!--</el-option>-->
                <!--</el-select>-->
            <!--</el-form-item>-->
            <el-form-item prop="password">
                <el-input type="password" v-model="form.password" auto-complete="on" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item prop="checkPassword">
                <el-input type="password" v-model="form.checkPassword" auto-complete="on" placeholder="确认密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addUser" style="width:47%; float:left;">确 定
                </el-button>
                <el-button type="primary" @click="signDialogVisible = false" style="width:47%; float:right;">取 消
                </el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
    import {requestLogin} from '../api/user';
    import {requestAddUser} from '../api/user';
    import md5 from 'js-md5'
    export default {
        name: "signDialog",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPassword !== '') {
                        this.$refs.form.validateField('checkPassword');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                // value: [],
                options:[],

                // optionsDepart: [{
                //     value: 'sea',
                //     label: '海洋部门'
                // }, {
                //     value: 'soil',
                //     label: '国土部门'
                // }, {
                //     value: 'public',
                //     label: '游客'
                // }],
                // valueDepart: '',
                // optionsLevel: [{
                //     value: '2',
                //     label: '处长'
                // }, {
                //     value: '1',
                //     label: '员工'
                // }],
                // valueLevel: '',
                signDialogVisible: false,
                logining: false,
                ruleForm2: {
                    account: '',
                    checkPass: ''
                },
                form: {
                    name: '',
                    password: '',
                    checkPassword: '',
                    // department: '',
                    // level: '',
                    newdepart:[]
                    // role:''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPassword: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                rules2: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    checkPass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
                checked: true
            };
        },
        methods: {
            handleChange(value) {
                console.log(value);
            },
            addUser() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        let addUserParams = {
                            name: this.form.name,
                            password: md5(this.form.password),
                            department: this.form.newdepart[0],
                            level: this.form.newdepart[1]
                        };
                        let that = this;
                        this.$axios.post(that.$URL.userRegister, addUserParams).then(function (res) {
                            if (res.code == 200) {
                                that.$message({
                                    message: "注册成功",
                                    type: 'success'
                                });
                                that.signDialogVisible = false;
                                that.$refs.form.resetFields();
                            }else if(res.code == 1003){
                                that.$message({
                                    message: "用户名存在，请重新填写信息",
                                    type: 'warning'
                                });
                                that.$refs.form.resetFields();
                            }else{
                                that.$message({
                                    message: "注册失败",
                                    type: 'error'
                                });
                                // that.signDialogVisible = false;
                                // that.$refs.form.resetFields();
                            }
                        });
                        // requestAddUser(addUserParams).then(res => {
                        //     let {message, code, body} = res.data;
                        //     if (res.status !== 200) {
                        //         this.$message({
                        //             message: message,
                        //             type: 'error'
                        //         });
                        //     } else {
                        //         window.alert("添加用户成功！");
                        //         this.dialogVisible = false;
                        //     }
                        // });
                    }
                });
            }


        },
        created() {
            let that = this;
            this.$Bus.$on('showSign', res => {
                that.signDialogVisible = res;
            });
            this.$axios.post(that.$URL.departInit).then(function (res) {
                if (res.code == 200) {
                  that.options=res.body;
                }
            });
        }
    }
</script>

<style>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }

    .remember {
        margin: 0px 0px 35px 0px;
    }

    .dialogTitle {
        margin: 0px auto 30px auto;
        text-align: center;
        color: #505458;
    }

    .el-form-item {
        margin-bottom: 18px;
    }
</style>